<?php

class Admin_Form_Listasimulado extends Zend_Form
{

    public function init()
    {
        Zend_Loader::loadClass('dominio','library');
        
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $id = $front->getRequest()->getParam('id', 0);

        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_pdr');
        
        if($id > 0){
           $query = 'SELECT SQL_CACHE * FROM LISTA_SIMULADO WHERE LTS_ID = ' . $id;
           $data = $db->fetchRow($query);
        }
        
        $el = new Zend_Form_Element_Hidden('LTS_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('LTS_NOM');
        $el->setLabel('Nome')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Textarea('LTS_DSC');
        $el->setLabel('Descrição');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        $query = 'SELECT SQL_CACHE FAC_ID,FAC_NOM FROM FACULDADE WHERE FAC_STA = 1 ORDER BY FAC_NOM';
        $row = $db->fetchAll($query);
        foreach($row as $r) $arr[$r['FAC_ID']] = $r['FAC_NOM']; 
        $el = new Zend_Form_Element_Select('FAC_ID',array('onchange' => ''));
        $el->setLabel('Instituições de Ensino');
        $el->addMultiOptions($arr);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        if($data['FAC_ID'] > 0){
            $query = 'SELECT SQL_CACHE FAP_ID,FAP_NOM FROM FACULDADE_PROVA '
                    .'WHERE FAC_ID = '.$data['FAC_ID'].' AND FAP_STA = 1 ORDER BY FAP_NOM';
            $row = $db->fetchAll($query);
            foreach($row as $r) $arr[$r['FAP_ID']] = $r['FAP_NOM']; 
        }
        $el = new Zend_Form_Element_Select('FAP_ID',array('onchange' => ''));
        $el->setLabel('Provas da Instituição de Ensino');
        $el->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        /*
        $el = new Zend_Form_Element_Text('LTS_DATENC');
        $el->setLabel('Data de Encerramento')->setRequired(true);
        $el->setAttrib('class', 'data');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('LTS_HORENC');
        $el->setLabel('Hora de Encerramento')->setRequired(true);
        $el->setAttrib('class', 'hora');
        $el->setAttrib('required', '');
        $this->addElement($el);
        */
        $tpo = 1;
        $arr_tpo = dominio::tipoAlternativa();
        $cmb_tpo = '<select name="tpo_res[]" class="tam_med tpo_res" >';
        foreach($arr_tpo as $key => $a_tpo){
            $selected = $tpo == $key ? 'selected="selected"':'';
            $cmb_tpo .= '<option '.$selected.' value="'.$key.'">'.$a_tpo.'</option>';
        }
        $cmb_tpo .= '</select>';
        
        $query = 'SELECT SQL_CACHE DCP_ID,DCP_NOM FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
        $result = $db->fetchAll($query);
        $cmb_dis = '<select name="d[]" class="tam_med_g dis_qst" ><option value="">Disciplina</option>';
        foreach($result as $row){
            $cmb_dis .= '<option value="'.$row['DCP_ID'].'">'.$row['DCP_NOM'].'</option>';
        }
        $cmb_dis .= '</select>';
        
        $num_qst = 0;
        $html_row = '';
        if($id > 0){
            $query = 'SELECT SQL_CACHE * FROM LISTA_SIMULADO_GABARITO WHERE LTS_ID = '.$id.' ORDER BY LSG_ID'; // LSG_QST
            $result = $db->fetchAll($query);
            foreach($result as $row){
                
                $arr_tpo = dominio::tipoAlternativa();
                $_cmb_tpo = '<select name="tpo_res[]" class="tam_med tpo_res" >';
                foreach($arr_tpo as $key => $a_tpo){
                    $selected = $row['LSG_TPO'] == $key ? 'selected="selected"':'';
                    $_cmb_tpo .= '<option '.$selected.' value="'.$key.'">'.$a_tpo.'</option>';
                }
                $_cmb_tpo .= '</select>';
                
                //$_DCP_ID = 0;
                $query = 'SELECT SQL_CACHE DCP_ID,DCP_NOM FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
                $_result = $db->fetchAll($query);
                $_cmb_dis = '<select name="d[]" class="tam_med_g dis_qst" style="display:none;"><option value="">Disciplina</option>';
                $_nome_dis = '';
                foreach($_result as $_row){
                    $selected = $row['DCP_ID'] == $_row['DCP_ID'] ? 'selected="selected"':'';
                    $_cmb_dis .= '<option '.$selected.' value="'.$_row['DCP_ID'].'">'.$_row['DCP_NOM'].'</option>';
                    //if($row['DCP_ID'] == $_row['DCP_ID']) $_DCP_ID = $_row['DCP_ID'];
                    if($row['DCP_ID'] == $_row['DCP_ID']) $_nome_dis = $_row['DCP_NOM'];
                }
                $_cmb_dis .= '</select>'.$_nome_dis;
                
                
                $num_qst = $row['LSG_QST']; 
                
                $html_row .= '<div class="cnt_el">';
                $html_row .= '<input type="hidden" name="stalngetg[]" value="'.$row['LSG_STALNGETG'].'">';
                $html_row .= '<input type="hidden" name="pes[]" value="'.$row['LSG_PES'].'">';
                $html_row .= '<span class="cnt_dis">'.$_cmb_tpo.'</span>';
                $html_row .= '<span class="cnt_num_qst">';
                $html_row .= ' <input type="text" class="num_qst" name="q[]" maxlength="3" value="'.$num_qst.'" readonly >';
                $html_row .= '</span>';

                $html_row .= '<span class="cnt_qst_alt">';
                
                $arr_qst = dominio::getAlternativa($row['LSG_TPO']);
                //$num_qst = 'I'.$key_tpo.'F';
                if(is_array($arr_qst)){
                    foreach($arr_qst as $key => $qst){ //  ($key == $row['LSG_ALT'] && $row['DCP_ID'] == $_DCP_ID)
                        $checked = $key == $row['LSG_ALT'] ? 'checked="checked"':'';
                        $class = $key == $row['LSG_ALT'] ? 'btn_qst_alt_act':'';
                        $html_row .= '<input '.$checked.' type="radio" id="'.$row['LSG_ID'].''.$key.'" class="rd_qst_alt" name="a['.$row['LSG_ID'].']" value="'.$key.'" >'; // id="'.$num_qst.''.$key.'" name="a['.$num_qst.']"
                        $html_row .= ' &nbsp; <label for="'.$row['LSG_ID'].''.$key.'"><span class="'.$class.' btn_qst_alt qst'.$num_qst.'" qst="'.$num_qst.'" val="'.$key.'">'.$qst.'</span></label> &nbsp; '; // for="'.$num_qst.''.$key.'"
                    }
                } else if($row['LSG_TPO'] == 3){
                    $html_row .= '<input type="text" id="'.$num_qst.''.$key.'" class="num_int numero" maxlength="3" name="a['.$num_qst.']" value="'.$row['LSG_ALT'].'" >';
                }
                
                $html_row .= '</span>';
                $html_row .= '<span class="cnt_dis txt_dis">'.$_cmb_dis.'</span>';
                $html_row .= '</div>';   
                
                
            }
        }
        $num_qst++;
        /*
        $html2 = '<div class="cnt_el">';
        $html2.= '<input type="hidden" name="stalngetg[]" value="0">';
        $html2 .= '<input type="hidden" name="pes[]" value="1">';
        $html2 .= '<span class="cnt_dis">'.$cmb_tpo.'</span>';
        $html2 .= '<span class="cnt_num_qst">';
        $html2 .= ' <input type="text" class="num_qst" name="q[]" maxlength="3" value="'.$num_qst.'" readonly >';
        $html2 .= '</span>';
        
        $html2 .= '<span class="cnt_qst_alt">';

        $arr_qst = dominio::getAlternativa($tpo);
        if(is_array($arr_qst)){
            foreach($arr_qst as $key => $qst){
                $html2 .= '<input type="radio" id="'.$num_qst.''.$key.'" class="rd_qst_alt" name="a['.$num_qst.']" value="'.$key.'" >';
                $html2 .= ' &nbsp; <label for="'.$num_qst.''.$key.'"><span class="btn_qst_alt qst'.$num_qst.'" qst="'.$num_qst.'" val="'.$key.'">'.$qst.'</span></label> &nbsp; ';
            }
        }else if($key_tpo == 3){
            $html2 .= '<input type="text" id="'.$num_qst.''.$key.'" class="num_int numero" maxlength="3" name="a['.$num_qst.']" value="" >';
        }
        $html2 .= '</span>';
        $html2 .= '<span class="cnt_dis">'.$cmb_dis.'</span>';
        //$html2 .= ' <a href="javascript:void(0)" class="cnf'.$num_qst.'" qst="'.$num_qst.'">Confirmar</a>';
        //$html2 .= ' <a href="javascript:void(0)" class="alt'.$num_qst.'" qst="'.$num_qst.'">Alterar</a>';
        $html2 .= '<span class="cnt_btn"><a href="javascript:void(0)" class="exc exc'.$num_qst.'" qst="'.$num_qst.'">Excluir</a></span>';
        $html2 .= '</div>'; 
        */
        $html = '<dt><label><h3>Gabarito</h3></label></dt>';
        $html .= '<div id="it_dis" class="lista-questoes">';
        $html .= $html_row;
        //if($html_row == '') $html .= $html2;
        $html .= '</div>';
        //$html .= '<div class="cnt_el"><input type="button" name="add" id="add" value="Adicionar Gabarito" class="btn-frm"></div>';
        
        
        $el = new Zend_Form_Element('html',array( 'helper'=>'formNote', 'value'=> $html ));
        $this->addElement($el);
        
        
        
        if($id > 0){
            $el = new Zend_Form_Element_Checkbox('LTS_STA',array('checked'=>'checked'));
            $el->setLabel('Ativo');
            $this->addElement($el);
        }
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
         
        $btn = new Zend_Form_Element_Button('Voltar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
    }


}

